package com.hongning.railway.ui.categorylistnews;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;

import com.hongning.railway.data.AppRepository;
import com.hongning.railway.entity.reqbean.ReqNewDetail;
import com.hongning.railway.entity.resbean.ResNewsDetail;
import com.hongning.railway.util.Apputils;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.ApiDisposableObserver;
import me.goldze.mvvmhabit.utils.RxUtils;

/**
 * Author: zhangjing
 * DATA:2019-08-14 16:03
 * DESC:
 * email:zhangjingworkmol@163.com
 */
public class NewDetailFragmentViewModel extends BaseViewModel<AppRepository> {
    public ObservableField<String> newsContent = new ObservableField<>();
    public UIChangeObserable uc =new UIChangeObserable();
    public NewDetailFragmentViewModel(@NonNull Application application, AppRepository model) {
        super(application, model);
    }

    public class UIChangeObserable{
        SingleLiveEvent<String> content = new SingleLiveEvent<String>();
    }

    public void requestDetail(String newsId){
        ReqNewDetail reqNewDetail=new ReqNewDetail();
        reqNewDetail.setNewsId(newsId);
        reqNewDetail.setUserId(Apputils.getLoginInfo().getUserinfo().getStaffId());
        model.getNewsDetail(reqNewDetail)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.bindToLifecycle(getLifecycleProvider()))
                .subscribe(new ApiDisposableObserver<ResNewsDetail>() {
                    @Override
                    public void onResult(ResNewsDetail resNewsDetail) {
                        if (resNewsDetail!=null&&resNewsDetail.getData()!=null){
                            newsContent.set(resNewsDetail.getData().getNewsContent());
                            uc.content.setValue(resNewsDetail.getData().getNewsContent());
                        }
                    }
                });
    }
}
